package com.farm.farming.service.impl;

import com.farm.entity.PageResult;
import com.farm.farming.dao.Cust_UserMapper;
import com.farm.farming.pojo.Cust_User;
import com.farm.farming.service.Cust_UserService;
import com.farm.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class Cust_UserServiceImpl implements Cust_UserService {

    @Autowired
    private Cust_UserMapper cust_userMapper;


    @Override
    public int add(Cust_User cust_user) {
        cust_userMapper.save(cust_user);

        return 1;
    }

    @Override
    public int delete(Integer id) {
        cust_userMapper.deleteById(id);
        return 1;
    }

    @Override
    public int update(Cust_User cust_user) {
        cust_userMapper.save(cust_user);
        return 1;
    }

    @Override
    public List<Cust_User> findAll() {
        return cust_userMapper.findAll();
    }

    @Override
    public PageResult<Cust_User> findPage(String query,Integer currentPage,Integer size) {
        //封装条件
        Cust_User cust_user = new Cust_User();
        cust_user.setName(query);
        cust_user.setType("0");
        Example<Cust_User> example = Example.of(cust_user, ExampleMatcher.matching()
                .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("type",ExampleMatcher.GenericPropertyMatchers.exact()));

        //分页查询条件
        PageRequest pageRequest = PageRequest.of(currentPage, size);
        Page<Cust_User> all = cust_userMapper.findAll(example, pageRequest);
        return new PageResult<>(all.getTotalElements(),all.getContent());

    }

    @Override
    public Cust_User findById(Integer id) {
        return cust_userMapper.getOne(id);
    }

    /**
     * 重置密码*/
    @Override
    public int updateById(Integer id) {
        cust_userMapper.update(id,MD5Utils.BCrypt("123456"));
        return 1;
    }


}
